//
//  StoreBarberViewController.m
//  HeadYang
//
//  Created by sunmingming on 15/7/18.
//  Copyright (c) 2015年 xiaoluoli. All rights reserved.
//

#import "StoreBarberViewController.h"
#import "BarberDetailViewController.h"
#import "UIImageView+AFNetworking.h"

@interface StoreBarberViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong) NSMutableArray *barberArray;
@property(nonatomic,weak) IBOutlet UITableView *listView;

@end

@implementation StoreBarberViewController

@synthesize storeDic;
@synthesize listView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"本店理发师";
        self.navigationItem.leftBarButtonItem =
        [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrow_white"]
                                         style:UIBarButtonItemStyleDone
                                        target:self
                                        action:@selector(backPressed:)];
        self.barberArray = [NSMutableArray array];
    }
    return self;
}

-(void)backPressed:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSDictionary *para =
    @{@"id":[self.storeDic valueForKey:@"id"],
      @"token":APPALL.myUserItem.token};
    [SVProgressHUD showWithStatus:@"loading..."];
    [AVCloud callFunctionInBackground:@"store_barber" withParameters:para block:^(id object, NSError *error) {
        // 执行结果
        if (!error && object) {
            NSLog(@"avget:%@",object);
            NSDictionary *aSucData = [NSJSONSerialization JSONObjectWithData: [object dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableLeaves error: &error];
            switch ([[aSucData valueForKey:@"success"] intValue]) {
                case 0:
                {
                    [tooles showGlobalAlertWithTitle:@"" andMsg:[aSucData valueForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                }
                    break;
                case 1:
                {
                    NSLog(@"yeah1....");
                    [_barberArray removeAllObjects];
                    NSArray *cityArray = [aSucData objectForKey:@"barber"];
                    for (NSDictionary *dic in cityArray) {
                        [_barberArray addObject:dic];
                    }
                    [listView reloadData];
                }
                    break;
                default:
                    break;
            }
            [SVProgressHUD dismiss];
        } else {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@", error]];
        }
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.barberArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100.0f;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *inde;
    inde= @"BarberID";
    cell = [tableView dequeueReusableCellWithIdentifier:inde];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"BarberCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    UILabel *lab1 = (UILabel*)[cell viewWithTag:1];
    UILabel *lab2 = (UILabel*)[cell viewWithTag:2];
    UILabel *lab3 = (UILabel*)[cell viewWithTag:3];
    UILabel *lab4 = (UILabel*)[cell viewWithTag:4];
    UIImageView *img1 = (UIImageView*)[cell viewWithTag:11];
    UIImageView *img2 = (UIImageView*)[cell viewWithTag:12];
    UIImageView *img3 = (UIImageView*)[cell viewWithTag:13];
    UIImageView *img4 = (UIImageView*)[cell viewWithTag:14];
    UIImageView *img5 = (UIImageView*)[cell viewWithTag:15];
    UIImageView *headImg = (UIImageView*)[cell viewWithTag:99];
    NSDictionary *cacheItem = [_barberArray objectAtIndex:indexPath.row];
    lab1.text = [tooles replaceNullFromDic:cacheItem withKeyArray:@[@"name"]];
    lab2.text = [NSString stringWithFormat:@"接单%@次",[tooles replaceNullFromDic:cacheItem withKeyArray:@[@"orderExperience"]]];
    lab3.text =  [tooles replaceNullFromDic:cacheItem withKeyArray:@[@"level"]];
    NSString *headImage = [cacheItem valueForKey:@"headImage"];
    if(![headImage isEqual:[NSNull null]]
       && headImage
       && headImage.length){
        [headImg setImageWithURL:[NSURL URLWithString:headImage]
                placeholderImage:[UIImage imageNamed:@"default_photo.png"]];
    }
    NSInteger score = [[cacheItem valueForKey:@"score"] integerValue];
    img1.hidden = YES;
    img2.hidden = YES;
    img3.hidden = YES;
    img4.hidden = YES;
    img5.hidden = YES;
    if(score > 0){img1.hidden = NO;}
    if(score > 20){img2.hidden = NO;}
    if(score > 40){img3.hidden = NO;}
    if(score > 60){img4.hidden = NO;}
    if(score > 80){img5.hidden = NO;}
    lab4.text = [NSString stringWithFormat:@"￥%@",[tooles replaceNullFromDic:cacheItem withKeyArray:@[@"averagePrice"]]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *cacheItem = [_barberArray objectAtIndex:indexPath.row];
    BarberDetailViewController *vc = [BarberDetailViewController new];
    vc.barberDic = cacheItem;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
